var app = getApp();
Component({
  properties: {
        adid : String,
        type : Number,
        img : String,
        tid : String,
        pageid : String,
    },

    data: {
        ad : null,
        isfirst : true,
        ising : true,
        chouing : false,
    },
    ready(){
        //this.playsuc();
    },
    methods: {
        showad(){
            var self = this;
            wx.showLoading({mask:true});
            if(self.data.type == 4){

                if(self.ad){
                    self.ad.show();
                    wx.hideLoading();
                    return false;
                }

                self.creareV();

            }
        },
        creareV(){

            var self = this;
            if(wx.createRewardedVideoAd){
                self.ad = wx.createRewardedVideoAd({ adUnitId: self.data.adid })
                self.ad.onLoad(() => {
                    wx.hideLoading();

                    if(!self.data.isfirst) return false;
                    self.data.isfirst = false;
                    self.ad.show();
                })
                self.ad.onError((err) => {
                    console.log('onError event emit', err)
                })
                self.ad.onClose((res) => {
                    wx.hideLoading();
                    if(res && res.isEnded){
                        self.playsuc();
                    }
                })
            }
        },
        playsuc(){
            var self = this;
            if( self.data.chouing ) return false;
            self.data.chouing = true;
            var postdata = {
                op : 'getxcxadp',
                frompage : 'temp',
                pageid : self.data.pageid,
                tid : self.data.tid,
            }
            app.com.http('user_v1','POST',postdata,0,true,'','',function(res){
                if ( res.data && !res.data.errno && res.data.errno !== undefined) {
                    app.com.toast(res.data.message);
                }else{
                    if(res.data.message) app.com.alert(res.data.message);
                }
                self.data.chouing = false;
            });
        }

    }

})